package cn.net.bhb.base.common.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by zkt on 2018-3-7.
 *
 * 就把它当个复刻版的AlertDialog
 */

public class AlertController {
    private AlertDialog dialog;
    private Window mWindow;

    private DialogViewHelper mViewHelper;

    public AlertController(AlertDialog dialog, Window window) {
        this.dialog = dialog;
        this.mWindow = window;
    }

    public AlertDialog getDialog() {
        return dialog;
    }

    public void setViewHelper(DialogViewHelper viewHelper) {
        this.mViewHelper = viewHelper;
    }

    public <T extends View> T getView(int viewId) {
        return mViewHelper.getView(viewId);
    }

    /**
     * 设置点击
     *
     * @param viewId
     * @param listener
     */
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        mViewHelper.setOnClickListener(viewId, listener);
    }

    /**
     * 设置文本
     *
     * @param viewId
     * @param text
     */
    public void setText(int viewId, CharSequence text) {
        mViewHelper.setText(viewId, text);
    }

    /**
     * 获取dialog的Window
     *
     * @return
     */
    public Window getWindow() {
        return mWindow;
    }

    public static class AlertParams {
        public Context mContext;
        public int mThemeResId;
        public boolean mCancelable = true; //点击空白是否能取消
        public DialogInterface.OnCancelListener mOnCancelListener; // Cancel监听
        public DialogInterface.OnDismissListener mOnDismissListener; // Dismiss监听
        public DialogInterface.OnKeyListener mOnKeyListener; // Key监听
        public View mViewLayout; // 布局View
        public int mViewLayoutResId; // 布局layoutId
        public SparseArray<CharSequence> mTextArray = new SparseArray<>();
        public SparseArray<View.OnClickListener> mClickArray = new SparseArray<>();
        public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
        public int mGravity = Gravity.CENTER;
        public int mAnimation = 0;


        public AlertParams(Context context, int themResId) {
            this.mContext = context;
            this.mThemeResId = themResId;
        }

        /**
         * 绑定和设置参数
         *
         * @param alert
         */
        public void apply(AlertController alert) {
            // 1. 设置布局
            DialogViewHelper viewHelper = null;
            if (mViewLayoutResId != 0) {
                viewHelper = new DialogViewHelper(mContext, mViewLayoutResId);
            }
            if (mViewLayout != null) {
                viewHelper = new DialogViewHelper();
                viewHelper.setContentView(mViewLayout);
            }

            if (viewHelper == null) {
                throw new IllegalArgumentException("请先设置布局 setContentView()");
            }

            // 设置DialogViewHelper
            alert.setViewHelper(viewHelper);

            // 设置dialog布局
            alert.getDialog().setContentView(viewHelper.getContentView());
            alert.getDialog().setCancelable(mCancelable);

            // 2. 设置文本
            int textArraySize = mTextArray.size();
            for (int i = 0; i < textArraySize; i++) {
                viewHelper.setText(mTextArray.keyAt(i), mTextArray.valueAt(i));
            }

            // 3. 设置点击
            int clickArraySize = mClickArray.size();
            for (int i = 0; i < clickArraySize; i++) {
                viewHelper.setOnClickListener(mClickArray.keyAt(i), mClickArray.valueAt(i));
            }

            // 4. 配置自定义的效果（全屏、从底部弹出、动画 等）
            Window window = alert.getWindow();

            window.setGravity(mGravity);
            if (mAnimation != 0) {
                window.setWindowAnimations(mAnimation);
            }
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = mWidth;
            attributes.height = mHeight;
            window.setAttributes(attributes);
        }
    }
}
